import 'dart:async';
import 'package:jpush_flutter/jpush_flutter.dart';
import 'dart:convert';
import 'package:redux/redux.dart';
import '../reducer/reducers.dart';
class PushUtil {
  Store<AppState> store;
  // static final PushUtil _instance = PushUtil._internal();
  static JPush _jpush;

  // factory PushUtil(pushServiceStore, playerStore) => _instance();

  PushUtil(Store<AppState> store) {
    if (null == _jpush) {
      this.store = store;
      this.init(store);
    }
  }

  init(Store<AppState> store) {
    _jpush = new JPush();
    _jpush.setup(
      appKey: "49fb62e70380805f7c25c1e7",
      channel: "theChannel",
      production: false,
      debug: true,
    );
    _jpush.getRegistrationID().then((regId) => {
      store.dispatch(new SetRegIdAction(regId: regId))
    });
    _jpush.applyPushAuthority(
        new NotificationSettingsIOS(sound: true, alert: true, badge: true));
    _jpush.addEventHandler(
      onReceiveNotification: (Map<String, dynamic> message) async {
        print("flutter onReceiveNotification: $message");
      },
      onOpenNotification: (Map<String, dynamic> message) async {
        print("flutter onOpenNotification: $message");
      },
      onReceiveMessage: (Map<String, dynamic> res) async {

        // print("flutter onReceiveMessage: ${res['message']}");
        // // final data = JsonEncoder(message['data']);
        final msg = jsonDecode(res['message']);
        if (msg['act'] == 'updateScreenInfo') {
          final Map screenInfo = msg['data'];
          store.dispatch(new UpdateScreenInfo(screenInfo: screenInfo));
        }
        // // print(msg['data']);
      },
    );
  }
}
